/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import java.util.ArrayList;
import java.util.List;
import xowl.gmi.environment.Locale;

/**
 *
 * @author Laurent WOUTERS
 */
public class ProjectNode extends WinProjectsNode {
    public static interface Actions {
        void onDiagramAdd();
        void onSave();
        void onSaveAs();
        void onClose();
        void onExecute(String id);
    }
    
    private Actions actions;
    private boolean modified;
    private ProjectTreeUpdater updater;
    private javax.swing.JPopupMenu menu;
    private javax.swing.JMenu menuExecute;
    private List<DiagramNode> onHold;
    
    public void setModified(boolean modified) {
        this.modified = modified;
        if (this.win != null)
            this.win.repaint();
    }
    public void setUpdater(ProjectTreeUpdater updater) {
        this.updater = updater;
        for (DiagramNode view : onHold)
            updater.insertNode(this, view);
        onHold.clear();
    }
    public javax.swing.JPopupMenu getMenu() { return menu; }
    
    public ProjectNode(Actions actions, Locale locale, String name) {
        super(name);
        this.actions = actions;
        this.menu = new javax.swing.JPopupMenu();
        javax.swing.JMenuItem itemNewDiagram = new javax.swing.JMenuItem(locale.fileNewDiagram_text, IconProvider.getEmbedded("diagram_add.png"));
        itemNewDiagram.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { ProjectNode.this.actions.onDiagramAdd(); }
        });
        javax.swing.JMenuItem itemSave = new javax.swing.JMenuItem(locale.fileSave_text, IconProvider.getEmbedded("save.png"));
        itemSave.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { ProjectNode.this.actions.onSave(); }
        });
        javax.swing.JMenuItem itemSaveAs = new javax.swing.JMenuItem(locale.fileSaveAs_text, IconProvider.getEmbedded("saveas.png"));
        itemSaveAs.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { ProjectNode.this.actions.onSaveAs(); }
        });
        javax.swing.JMenuItem itemClose = new javax.swing.JMenuItem(locale.fileCloseProject_text, IconProvider.getEmbedded("project_close.png"));
        itemClose.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { ProjectNode.this.actions.onClose(); }
        });
        menuExecute = new javax.swing.JMenu(locale.runMenu_text);
        menuExecute.setIcon(IconProvider.getEmbedded("execute.png"));
        
        this.menu.add(itemNewDiagram);
        this.menu.add(itemSave);
        this.menu.add(itemSaveAs);
        this.menu.add(itemClose);
        
        this.onHold = new ArrayList<>();
    }
    
    public void addExecutionChoice(final String id, String name) {
        javax.swing.JMenuItem item = new javax.swing.JMenuItem(name, IconProvider.getEmbedded("play.png"));
        item.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { actions.onExecute(id); }
        });
        menuExecute.add(item);
        if (menu.getComponentIndex(menuExecute) == -1) {
            menu.addSeparator();
            menu.add(menuExecute);
        }
    }
    
    public void addDiagram(DiagramNode diagram) {
        if (updater != null)
            updater.insertNode(this, diagram);
        else
            onHold.add(diagram);
    }
    
    public void removeDiagram(DiagramNode diagram) {
        updater.removeNode(diagram);
    }
    
    @Override public java.awt.Component getRendering(ViewStyle style, boolean selected) {
        javax.swing.JLabel label = new javax.swing.JLabel(getUserObject().toString(), IconProvider.getEmbedded("project.png"), javax.swing.JLabel.TRAILING);
        label.setFont(new java.awt.Font(style.fontName, 0, style.fontSize));
        if (modified)
            label.setForeground(style.projectsModifiedColor);
        if (selected) {
            label.setOpaque(true);
            label.setBackground(style.projectsSelectedColor);
        }
        label.setComponentPopupMenu(menu);
        return label;
    }
}
